<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import { getLoanAidStatistics } from '@/api/home'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      creditSuccess: [],
      numberOfFormsSubmittedSuccessfully: [],
      numberOfLoansApproved: [],
      timeCount: [],
      numberOfPasses: [],
      numberOfSuccessfulLoans: [],
      testc: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      y1: [1, 7, 3, 4, 5, 3, 5],
      y2: [3, 2, 8, 9, 5, 4, 4],
      y3: [4, 9, 3, 4, 5, 6, 2]
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.getLoanAidStatistics()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    // 获取用户列表
    async getLoanAidStatistics() {
      const res = await getLoanAidStatistics(this.listQuery)

      this.creditSuccess = res.data.data[0].creditSuccess
      this.numberOfFormsSubmittedSuccessfully = res.data.data[0].numberOfFormsSubmittedSuccessfully
      this.numberOfLoansApproved = res.data.data[0].numberOfLoansApproved
      this.numberOfPasses = res.data.data[0].numberOfPasses
      this.numberOfSuccessfulLoans = res.data.data[0].numberOfSuccessfulLoans
      this.numberOfSuccessfulRepayment = res.data.data[0].numberOfSuccessfulRepayment
      this.overCounting = res.data.data[0].overCounting
      this.passingNumberOfFrontScreen = res.data.data[0].passingNumberOfFrontScreen



      for (let i = 0; i < this.creditSuccess.length; i++) {
        this.timeCount[i] = this.creditSuccess[i].datas.substr(5, 11)
        this.creditSuccess[i] = this.creditSuccess[i].count
      }
      for (let j = 0; j < this.numberOfFormsSubmittedSuccessfully.length; j++) {
        this.numberOfFormsSubmittedSuccessfully[j] = this.numberOfFormsSubmittedSuccessfully[j].count
      }
      for (let k = 0; k < this.numberOfLoansApproved.length; k++) {
        this.numberOfLoansApproved[k] = this.numberOfLoansApproved[k].count
      }
      for (let o = 0; o < this.numberOfPasses.length; o++) {
        this.numberOfPasses[o] = this.numberOfPasses[o].count
      }
      for (let t = 0; t < this.numberOfSuccessfulLoans.length; t++) {
        this.numberOfSuccessfulLoans[t] = this.numberOfSuccessfulLoans[t].count
      }
      for (let t = 0; t < this.numberOfSuccessfulRepayment.length; t++) {
        this.numberOfSuccessfulRepayment[t] = this.numberOfSuccessfulRepayment[t].count
      }
      for (let t = 0; t < this.overCounting.length; t++) {
        this.overCounting[t] = this.overCounting[t].count
      }
      for (let t = 0; t < this.passingNumberOfFrontScreen.length; t++) {
        this.passingNumberOfFrontScreen[t] = this.passingNumberOfFrontScreen[t].count
      }
      this.initChart()
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        xAxis: {
          data: this.timeCount,
          boundaryGap: true, // 不从坐标0开始
          axisTick: {
            show: false
          }
        },
        title: {
          text: '近七天助贷数据统计',
          x: 'center',
          y: 'top',
          padding: [10, 0]
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 90,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          top: '9%',
          x: 'center',
          itemGap: 20,
          data: ['前筛通过数', '准入通过数', '表单提交成功数', '授信成功数', '绑卡通过数', '放款审核通过数', '放款成功数', '还款成功数']
        },
        series: [
          {
            name: '前筛通过数', itemStyle: {
              normal: {
                color: '#40c9c6',
                lineStyle: {
                  color: '#40c9c6',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: this.passingNumberOfFrontScreen,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
          {
            name: '准入通过数',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#f4516c',
                lineStyle: {
                  color: '#f4516c',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: this.numberOfPasses,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '表单提交成功数',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#6cff00',
                lineStyle: {
                  color: '#6cff00',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: this.numberOfFormsSubmittedSuccessfully,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '授信成功数',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#fff59a',
                lineStyle: {
                  color: '#fff59a',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: this.creditSuccess,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '绑卡通过数',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#5f5f5f',
                lineStyle: {
                  color: '#5f5f5f',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: this.overCounting,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '放款审核通过数',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#38fff8',
                lineStyle: {
                  color: '#38fff8',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: this.numberOfLoansApproved,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '放款成功数',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#36a3f7',
                lineStyle: {
                  color: '#36a3f7',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: this.numberOfSuccessfulLoans,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '还款成功数',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#ffa427',
                lineStyle: {
                  color: '#ffa427',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: this.numberOfSuccessfulRepayment,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          }]
      })
    }
  }
}
</script>
